package com.yunshang.ys.ui.access;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.yunshang.ys.MainActivity;
import com.yunshang.ys.R;
import com.yunshang.ys.api.AccessApi;
import com.yunshang.ys.base.BaseActivity;
import com.yunshang.ys.bean.UserBean;
import com.yunshang.ys.utils.UserInfo;
import com.yunshang.ys.utils.Validator;
import com.yunshang.ys.view.ImageViewPlus;
import com.yunshang.ys.ys.YSHttp;
import com.yunshang.ys.ys.YSSubscriber;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class LoginRegisterActivity extends BaseActivity {


    ImageViewPlus ivhead;
    Button btnRegister;
    Button btnLogin;
    EditText etAcc;
    private EditText etPass;
    private CheckBox cbPass;
    private CheckBox cbAcc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_register);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

        initstatusbar();


        ivhead = (ImageViewPlus) findViewById(R.id.iv_head);
        ivhead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LoginRegisterActivity.this, MainActivity.class));
            }
        });

        btnRegister = (Button) findViewById(R.id.btn_register);
        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LoginRegisterActivity.this, RegisterActivity.class));
            }
        });
        btnLogin = (Button) findViewById(R.id.btn_login);
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkformat();
            }

        });


        etAcc = (EditText) findViewById(R.id.et_account);
        etPass = (EditText) findViewById(R.id.et_pass);
        cbPass = (CheckBox) findViewById(R.id.cb_pass);
        cbAcc = (CheckBox) findViewById(R.id.cb_account);

        if (UserInfo.getAccountCH()) {
            etAcc.setText(UserInfo.getAccount());
            cbAcc.setChecked(true);
        }
        if (UserInfo.getPasswordCH()) {
            etPass.setText(UserInfo.getPassword());
            cbPass.setChecked(true);
        }


    }

    /**
     * 判断各式
     */
    private synchronized void checkformat() {
        String accstr = etAcc.getText().toString().trim();
        String passstr = etPass.getText().toString().trim();
        if (!TextUtils.isEmpty(accstr)) {
            if (Validator.isEmail(accstr) || Validator.isMobile(accstr)) {
                if (!TextUtils.isEmpty(passstr)) {
                    if (Validator.isPassword(passstr)) {


                        netLogin(accstr, passstr);


                    } else Toast.makeText(this, "请输入正确的密码", Toast.LENGTH_SHORT).show();
                } else Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
            } else Toast.makeText(this, "请输入正确的账号", Toast.LENGTH_SHORT).show();
        } else Toast.makeText(this, "请输入账号", Toast.LENGTH_SHORT).show();
    }

    /**
     * 登陆接口
     */
    private void netLogin(final String acc, final String pass) {
        showProgressDialog("登录中...", false);
        YSHttp.createApi(AccessApi.class)
                .netLogin(acc, pass)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new YSSubscriber<UserBean>() {
                    @Override
                    public void onSuccess(UserBean t, String msg) {
                        System.out.println("rrrrrrrrrrrr " + t);
                        System.out.println("tttttttttttt " + msg);
                        if (cbAcc.isChecked()) {
                            UserInfo.setAccountCH(true);
                            UserInfo.setAccount(acc);
                        } else UserInfo.setAccountCH(false);

                        if (cbPass.isChecked()) {
                            UserInfo.setPasswordCH(true);
                            UserInfo.setPassword(pass);
                        } else UserInfo.setPasswordCH(false);


                        Toast.makeText(LoginRegisterActivity.this, msg, Toast.LENGTH_SHORT).show();
                        startActivity(new Intent(LoginRegisterActivity.this, MainActivity.class));
                        finish();
                        dismissProgressDialog();
                    }

                    @Override
                    public void onFail(String msg) {
                        dismissProgressDialog();
                        System.out.println("msgmsgmsgmsg " + msg);
                        Toast.makeText(LoginRegisterActivity.this, msg, Toast.LENGTH_SHORT).show();
                    }
                });
    }

}
